package fr.pud.client.controller.routes;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Routes;

/**
 * Apr 10, 2012 - tsp-osmOSE.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         LoadQueries.java
 */
public class LoadRoutes extends AbstractAction {
    /**
     * 
     */
    private static final long serialVersionUID = -8774825899716395369L;

    public LoadRoutes() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.LOAD_ROUTES));
        this.putValue(SMALL_ICON, ImagesDatabase.LOAD);
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.LOAD_ROUTES));
    }

    /*
     * (non-Javadoc)
     * @see
     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        File f;
        JFileChooser explorer;
        explorer = new JFileChooser(
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.FOLDER_NAME_PUD_DATA));
        FileFilter filterText = new FileNameExtensionFilter(
                LanguagesConstants.LANGUAGE.getString(LanguagesConstants.FILE)
                        + ": " + "routes", "routes");
        explorer.addChoosableFileFilter(filterText);
        explorer.setFileSelectionMode(JFileChooser.FILES_ONLY);
        /*
         * Getting the file
         */
        explorer.showDialog(
                null,
                LanguagesConstants.LANGUAGE.getString(LanguagesConstants.OPEN)
                        + " "
                        + LanguagesConstants.LANGUAGE
                                .getString(LanguagesConstants.ROUTES));
        if (explorer.getSelectedFile() != null) {
            f = explorer.getSelectedFile();
            Routes.getInstance().setRoutes(Routes.loadRoutes(f));
        }
    }
}
